/*
    This class will load and save plugins, etc ... also this class will be responsible for creating
 * plugins ... we can add plugins using addplugins, and run them as we see fit.
 
 */

// Todo: Duplicate Checking, Check for Buddies with the same name, Make sure buddies which are
//       Not classes will not be added, etc.

package StudyBuddy;
import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author Gabriel
 */
public class PluginCollection {

ArrayList<Plugin> collection = new ArrayList<Plugin>();
// Expandable Array of plugins - added to plugin collection

    public void writeListToFile(){
        
        try {
            
            BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
            for(int i =0; i<collection.size(); i++){
                System.out.println(collection.get(i).toString());
                out.write(collection.get(i).toString() + "\n");
            }
            out.close();
            
        } 
        
        catch (IOException e) {
        }
        
    }
    
    
    public void loadListFromFile() throws Exception{
        
        // Description: This loads the list from a file
        // If there are any existing plugins already installed, it is cleared
        
        try {
            collection.clear(); // First clears the collection ... if there's anything in it.
            System.out.println("NOW LOADING FILES");
            BufferedReader in = new BufferedReader(new FileReader("outfilename"));
            String str = "";
            while (str != null) {
                str = in.readLine();
 //               System.out.println(str);
                if (str != null){
                    System.out.println(str);
                    Plugin temp = new Plugin(new File(str));
                    temp.loadPlugin(new File(str));
                    System.out.println(temp.toString());
                    collection.add(temp);
                }
            }
            in.close();
        } 
        
        catch (IOException e) {
        }
        
    }
    
    public void PluginCollection(){
        
        //collection = new Plugin[10];
        
    }
    
    public void removePlugin(int index){
        
        collection.remove(index);
        
    }
    
    
    public void removePlugin(Plugin theplugin){
        
        collection.remove(theplugin);
        
    }
    
    public void addPlugin(File filename) throws Exception{
        
        // Description: Adds a plugin to the index
        // Returns true if plugin has been added sucessfully, false otherwise
        
        Plugin temp = new Plugin(filename);
        temp.loadPlugin(filename);     
        collection.add(temp);
        
        
    }
    
    
    public String getPluginName(int index){
         
        return collection.get(index).getName();
        
    }
    
    public void runPlugin(int index){
        
        // Description: Runs a plugin based on it's index
        
        if(index >= 0 && index < collection.size()){
            collection.get(index).runplugin();
        }
                
    }
 
    
    public Plugin getPlugin(int index){
        
        // Description: Runs a plugin based on it's index
        
        if(index >= 0 && index < collection.size()){
            return collection.get(index);
        }
        
        return null;
                
    }
    
    public void runPlugin(String query){
    
        // Description: Runs a plugin based on it's name
        
        for(int i =0; i<collection.size(); i++){
            if(collection.get(i).getName().equals(query)){
                collection.get(i).runplugin();
            }
        }
    }
    
    public int getSize(){
        
        // Returns the size of the plugin collection
        return collection.size();
        
    }
}
